/*
 * Copyright (c) 2012-2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.ide.projecttype.wizard;

import com.google.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.validation.constraints.NotNull;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.project.type.wizard.ProjectWizardRegistrar;
import org.eclipse.che.ide.api.project.type.wizard.ProjectWizardRegistry;
import org.eclipse.che.ide.util.loging.Log;

/**
 * Implementation for {@link ProjectWizardRegistry}.
 *
 * @author Artem Zatsarynnyi
 */
public class ProjectWizardRegistryImpl implements ProjectWizardRegistry {
  private static final String DEFAULT_CATEGORY = "Other";
  private final Map<String, ProjectWizardRegistrar> registrars;

  public ProjectWizardRegistryImpl() {
    registrars = new HashMap<>();
  }

  @Inject(optional = true)
  private void register(Set<ProjectWizardRegistrar> registrars) {
    for (ProjectWizardRegistrar registrar : registrars) {
      final String id = registrar.getProjectTypeId();
      if (this.registrars.containsKey(id)) {
        Log.warn(
            ProjectWizardRegistryImpl.class,
            "Wizard for project type " + id + " already registered.");
      } else {
        this.registrars.put(id, registrar);
      }
    }
  }

  @Nullable
  @Override
  public ProjectWizardRegistrar getWizardRegistrar(@NotNull String projectTypeId) {
    return registrars.get(projectTypeId);
  }

  @Nullable
  @Override
  public String getWizardCategory(@NotNull String projectTypeId) {
    ProjectWizardRegistrar registrar = registrars.get(projectTypeId);
    if (registrar != null) {
      final String category = registrar.getCategory();
      return category.isEmpty() ? DEFAULT_CATEGORY : category;
    }
    return null;
  }
}
